package com.gohi.main.store.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

//import com.caucho.server.http.HttpRequest;
import org.apache.http.HttpRequest;
import com.gohi.core.web.action.BaseAction;
import com.gohi.main.store.hbm.ShopgooBeautician;
import com.gohi.main.store.hbm.ShopgooComment;
import com.gohi.main.store.hbm.ShopgooGroupbuy;
import com.gohi.main.store.hbm.ShopgooSpecialoffer;
import com.gohi.main.store.hbm.ShopgooStore;
import com.gohi.main.store.service.ICommentService;
import com.gohi.main.store.service.IDiscountService;
import com.gohi.main.store.service.IStoreService;
import com.gohi.main.system.hbm.ShopgooArea;
import com.gohi.main.system.service.ISystemService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.util.AppJsonUtils;
import com.gohi.main.util.StringUtil;
import com.gohi.main.util.Utils;
import com.gohi.main.util.WebUtils;
import com.gohi.main.util.Cache.MainCache;

import com.gohi.main.wxapi.util.AccoutUtil;
import com.gohi.main.wxapi.vo.JsApi;
import com.gohi.main.wxapi.vo.WxAccount;

/**
 * 商户相关的ACTION
 * 
 * @author Administrator
 * 
 */
public class StoreAction extends BaseAction {
	Logger logger = Logger.getLogger(StoreAction.class);
	private ISystemService systemService;
	// 商户
	private IStoreService storeService;
	// 商铺项目
	private ShopgooGroupbuy group;
	//特惠信息模块
	private IDiscountService discountService;
	// 留言信息

	private ICommentService commentService;
	private ShopgooStore s;

	// 测试地区
	public void getare() {
		AppJsonUtils.ptinrJson(systemService.getAre(), getRequest(),
				getResponse());
	}

	// 商铺首页
	public String index() throws IOException {
		String type = getRequest().getParameter("type");// 标示是否AJAX请求来的
	
		if (s == null) {
			// 初始化
			s = new ShopgooStore();
		}

		// 根据城市查询
		Cookie ck = WebUtils.getCookieByName(getRequest(), "city");
		if (ck == null) {
			s.setCity_id(67);// 默认上海
			WebUtils.addCookie(getResponse(), "city", "67-上海", -1);// 默认上海
		} else {
			s.setCity_id(Integer.parseInt(ck.getValue().split("-")[0]));// 默认上海
		}
		// s.set
		Cookie point = WebUtils.getCookieByName(getRequest(), "point");
		if (point == null) {
			s.setLat("31.229670");
			s.setLng("121.476200");
		} else {
			String sp[] = point.getValue().split("-");
			s.setLat(sp[1]);
			s.setLng(sp[0]);
			// System.err.println("使用定位坐标");
		}
		// if()
		
		
		if (type != null && type.equals("ajax")) {
			// System.err.println(group.getLimit());
			List list = storeService.queryStore(s);
			AppJsonUtils.ptinrJson(list, getRequest(), getResponse());
			return null;
		}
		
		
		
		List list = storeService.queryStore(s);
		this.jsonString = AppJsonUtils.getJson(list);
		
		
		
		// 区域的
		this.getRequest().setAttribute("are", MainCache.getArealist());
		// 查询类型
		this.getRequest().setAttribute("sclass", MainCache.getClasslist());
		// 查询品牌的
		this.getRequest().setAttribute("brand", MainCache.getBrandlist());
		// 查询商圈
		this.getRequest().setAttribute("business",MainCache.getBusinessmap(s.getCity_id()));
		// 查询下面的区域
		this.getRequest().setAttribute("aremap",MainCache.getAreaMap(s.getCity_id()));
		
		// System.err.println(list.size());
		return "index";
	}

	// 首页服务页面
	public String services() throws IOException {
		String type = getRequest().getParameter("type");// 标示是否AJAX请求来的
	
		if (group == null) {
			group = new ShopgooGroupbuy();
		}
		// 查询cookie
		Cookie ck = WebUtils.getCookieByName(getRequest(), "city");
		if (ck == null) {
			group.setCity_id(67);// 默认上海
			WebUtils.addCookie(getResponse(), "city", "67-上海", -1);// 默认上海
		} else {
			group.setCity_id(Integer.parseInt(ck.getValue().split("-")[0]));// 默认上海
		}
		
		
		if (type != null && type.equals("ajax")) {
			// System.err.println(group.getLimit());
			List list = storeService.queryGroup(group);
			AppJsonUtils.ptinrJson(list, getRequest(), getResponse());
			return null;
		}
		
		
		// 数据JOSN
		List list = storeService.queryGroup(group);
		this.jsonString = AppJsonUtils.getJson(list);
		//System.err.println(.size());

		// JSONObject js =
		// JSONObject.fromObject(systemService.getAreByparid(group.getCity_id()+""));
		// System.err.println(js.toString());
		
		
		
		//#########################################查询缓存里面的
		// 区域的
		this.getRequest().setAttribute("are", MainCache.getArealist());
		// 查询类型
		this.getRequest().setAttribute("sclass", MainCache.getClasslist());
		// 查询品牌的
		this.getRequest().setAttribute("brand", MainCache.getBrandlist());
		// 查询商圈
		this.getRequest().setAttribute("business",MainCache.getBusinessmap(group.getCity_id()));
		// 查询下面的区域
		this.getRequest().setAttribute("aremap",MainCache.getAreaMap(group.getCity_id()));
		return "services";
	}

	// 商铺详细信息
	public String detail() {
		// 根据ID查询
		String id = getRequest().getParameter("id");
		if (!Utils.show(id).equals("")) {
			s = storeService.getStoreById(id);
			// 查询这个商铺的服务项目
			List<ShopgooGroupbuy> group = storeService
					.queryGroup(new ShopgooGroupbuy(Integer.parseInt(id)));
			// System.out.println(group.size());
			this.getRequest().setAttribute("group", group);

			//判断这个 商铺是否已经收藏了！
			ShopgooMember user=  (ShopgooMember) this.getRequest().getSession().getAttribute("user");
			if(user!=null){
			  this.getRequest().setAttribute("isfav", storeService.favThere("store", id, user.getMember_id().toString()));
			}
			
			//查询商品的特惠信息
			ShopgooSpecialoffer spec =new ShopgooSpecialoffer();
			spec.setStore_id(Integer.parseInt(id));
			this.getRequest().setAttribute("dislist", discountService.queryDiscount(spec));
			return "detail";
		} else {
			return "ERROR";
		}

	}

	// 技师列表页面
	public String emlist() {
		// 店铺的ID
		String id = getRequest().getParameter("id");
		if (!Utils.show(id).equals("")) {
			this.getRequest().setAttribute("list",
					storeService.queryBeauticianByStroe(id));
			return "emlist";
		} else {
			return "ERROR";
		}
	}

	// 店铺详细信息
	public String emdetail() {
		// 技师的ID
		String id = getRequest().getParameter("id");
		if (!Utils.show(id).equals("")) {
			// 查询单个技师
			ShopgooBeautician bea = storeService.queryBeauticianByid(id);
			if (bea != null) {
				// 查询单个技师
				getRequest().setAttribute("be", bea);
				// 查询这个技师下面的服务
				getRequest().setAttribute(
						"group",
						storeService.queryGroupbuyByBeauticId(bea
								.getBeautician_id().toString()));
				// 查询技师的留言信息
				List<ShopgooComment> list = commentService.getCommentByType(
						"3", id);

				for (ShopgooComment c : list) {
					c.setAmount_score(c.getBeautician_score());
				}
				getRequest().setAttribute("list", list);
				return "emdetail";
			} else {
				return "ERROR";
			}

			// System.err.println(bea.getStore_name());

		}
		return "ERROR";
	}

	// 服务类型
	public String servicesdetail() throws NumberFormatException, ParseException {
		if (group != null) {
			// 查询单个服务
			// System.err.println(storeService.queryGroup(group).size());
			List<ShopgooGroupbuy> list = storeService.queryGroup(group);
			if(list.size()==0){
				this.alert="参数错误！";
				 return "ERROR";
			}
			group = list.get(0);
			// 查询商铺
			s = storeService.getStoreById(group.getStore_id() + "");
			// 根据这个服务查询里面的技师
			this.getRequest().setAttribute(
					"list",
					storeService.queryBeauticianByGroupId(group.getGroup_id()
							+ ""));
			HttpServletRequest req = this.getRequest();
			if (req.getParameter("beauticianid") != null) {
				this.getRequest().setAttribute("beauticianid",
						req.getParameter("beauticianid"));
				// 技师的名字
				this.getRequest().setAttribute("beauticianname",
						req.getParameter("beauticianname"));
				// 技师的价格
				this.getRequest().setAttribute("beauticiaprice",
						req.getParameter("beauticiaprice"));

			}
			// 计算默认的时间

			String text = StringUtil.splitTime(StringUtil.parentSimple(s.getBusiness_starttime(), "HH:mm")
					, StringUtil.parentSimple(s.getBusiness_endtime(), "HH:mm"),group.getHowlong());
			
			
			String text1 = StringUtil.splitTime(StringUtil.parentSimple(s.getBusiness_starttime(), "HH:mm")
					, StringUtil.parentSimple(s.getBusiness_endtime(), "HH:mm"),group.getHowlong(),0);
			
			
			//System.err.println(text);
			Map<String, String> map = new LinkedHashMap<String, String>();
			String d = StringUtil.getDate("yyyy-MM-dd");
			// 循坏四天
			for (int i = 0; i <= 3; i++) {
				String key = StringUtil.getNextDate(d, i);
				if (!map.containsKey(key)) {
					if(i==0){
						//第一个表示今天 要对时间进行处理
						map.put(key, text1);
					}else{
						map.put(key, text);
					}
			
				}
			}
			// 转成JSON到客户端渲染
			JSONObject js = JSONObject.fromObject(map);
			this.jsonString = js.toString();
			// System.err.println(js.toString());

			return "servicesdetail";
		}

		return "ERROR";

	}

	// 评论信息
	public String commentlist() {
		// 1商铺评论 2服务评论 3技师评论
		String type = getRequest().getParameter("type");
		String id = getRequest().getParameter("id");
		if (!Utils.show(type).equals("") && !Utils.show(id).equals("")) {
			List<ShopgooComment> list = commentService.getCommentByType(type,
					id);

			for (ShopgooComment c : list) {
				if (type.equals("1")) {
					c.setAmount_score(c.getStore_score());
				} else if (type.equals("2")) {
					c.setAmount_score(c.getItem_score());
				} else if (type.equals("3")) {
					c.setAmount_score(c.getBeautician_score());
				}
			}
			getRequest().setAttribute("list", list);
			// System.err.println(list.size());
			return "commentlist";
		}
		// 空指针错误
		return "ERROR";
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public IStoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(IStoreService storeService) {
		this.storeService = storeService;
	}

	public ShopgooStore getS() {
		return s;
	}

	public void setS(ShopgooStore s) {
		this.s = s;
	}

	public ShopgooGroupbuy getGroup() {
		return group;
	}

	public void setGroup(ShopgooGroupbuy group) {
		this.group = group;
	}

	public ICommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(ICommentService commentService) {
		this.commentService = commentService;
	}

	public IDiscountService getDiscountService() {
		return discountService;
	}

	public void setDiscountService(IDiscountService discountService) {
		this.discountService = discountService;
	}
	
}
